package cn.xn.hrms.domain.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import cn.xn.hrms.api.dto.res.PositionDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.xn.hrms.api.dto.req.PositionReqDTO;
import cn.xn.hrms.infrastructure.dataobject.PersonalDO;
import cn.xn.hrms.infrastructure.dataobject.PositionSelectDO;
import cn.xn.hrms.infrastructure.dataobject.PositionDO;
import cn.xn.hrms.domain.enums.ResultEnum;
import cn.xn.hrms.domain.exception.BaseException;
import cn.xn.hrms.domain.service.DepartmentService;
import cn.xn.hrms.domain.service.PersonalService;
import cn.xn.hrms.domain.service.PositionService;
import cn.xn.hrms.infrastructure.dao.PositionDao;

/**
 * @author ysz
 */
@Service
public class PositionServiceImpl implements PositionService {

    @Resource
    private PositionDao positionDao;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private PersonalService personalService;

    @Override
    public PageInfo<PositionDO> findAll(int pageNum, int pageSize, int departmentId, String positionName) {
        if (ObjectUtils.isEmpty(positionName)) {
            positionName = null;
        }
        if (departmentId != 0) {
            departmentService.find(departmentId);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<PositionDO> positionDOS = positionDao.selectAll(departmentId, positionName);
        return new PageInfo<>(positionDOS);
    }

    @Override
    public List<PositionDO> all() {
        return positionDao.selectAll(0, null);
    }

    @Override
    public PositionDTO find(int id) {
        PositionDTO positionDTO = positionDao.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(positionDTO)) {
            throw new BaseException(ResultEnum.POSITION_NOT_EXIST);
        }
        return positionDTO;
    }

    @Override
    public void insert(PositionReqDTO positionReqDTO) {
        departmentService.find(positionReqDTO.getDepartmentId());
        PositionDTO positionDTO = new PositionDTO();
        BeanUtils.copyProperties(positionReqDTO, positionDTO);
        positionDao.insertSelective(positionDTO);
    }

    @Override
    public void deleteByIdIn(Collection<Integer> idList) {
        //校验该岗位有没有被绑定，绑定了无法删除
        final List<PersonalDO> personalAll = personalService.All();
        personalAll.forEach(personalVo -> idList.forEach(id -> {
            if (id.equals(personalVo.getPositionId())) {
                throw new BaseException(ResultEnum.POSITION_EXIST_PERSONAL);
            }
        }));
        positionDao.updateIsDeletedByIdIn(1, idList);
    }

    @Override
    public void updateById(Integer id, PositionReqDTO positionReqDTO) {
        if (ObjectUtils.isEmpty(positionReqDTO.getDepartmentId())) {
            throw new BaseException(ResultEnum.DEPARTMENT_NOT_EXIST);
        }
        departmentService.find(positionReqDTO.getDepartmentId());
        PositionDTO positionDTO = positionDao.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(positionDTO)) {
            throw new BaseException(ResultEnum.POSITION_NOT_EXIST);
        }
        BeanUtils.copyProperties(positionReqDTO, positionDTO);
        positionDTO.setPositionId(id);
        positionDao.updateByPrimaryKeySelective(positionDTO);
    }

    @Override
    public List<PositionSelectDO> findSelect(Integer departmentId) {
        departmentService.find(departmentId);
        return positionDao.selectAllByDepartmentId(departmentId);
    }
}
